<?php
class ext_barang_model extends MY_Model {
	var $nama_tabel = 'ext_barang';
	
	function GetJSON() {
		$ls_sql	= "	SELECT	a.*,b.nama_satuan,c.nama_kondisi
					FROM	".$this->nama_tabel." a
					left join ext_satuan b on a.id_satuan_fk = b.id_satuan_pk
					left join ext_kondisi c on a.id_kondisi_fk = c.id_kondisi_pk
					WHERE	(UPPER(id_barang_pk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(NVL(ket_barang,'')) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'nama_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
					
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_barang_pk' => $arr_rule['id_barang_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GenerateId() {
		$sql = "select id_barang_pk 
				from ( 
					select * 
					from ext_barang
					order by id_barang_pk desc
					)
				where ROWNUM <= 1
				";
		$data = $this->db->query($sql)->row_array();

		if (count($data) == 0) {
			return 1;
		} else {
			return intval($data['id_barang_pk'])+1;
		}
	}

}
?>